package com.topvision.myviewtest.kotlin.part1

/**
 *
 * @author lucy
 * @date 2018-05-29 13:22
 * @description //基本函数使用 ， 字符串模板
 */

// 简单函数定义
/*
fun sum(a: Int, b: Int): Int {
    return a + b
}

fun main(args: Array<String>) {
    print("sum 1 and 7 is")
    println(sum(1, 7))
    // out: sum 1 and 7 is 8
}
*/

// 返回值自动推断-函数定义
/*
fun sum(a: Int, b: Int) = a + b

fun main(args: Array<String>) {
    print("sum of 1 and 7 is ${sum(1, 7)}")
    // out: sum 1 and 7 is 8
}
*/

// 函数返回无意义值(可以省略)
/*
//fun printSum(a: Int, b: Int): Unit {
//    print("sum $a and $b is ${a + b}")
//}

fun printSum(a: Int, b: Int) {
    print("sum $a and $b is ${a + b}")
}

fun main(args: Array<String>) {
    printSum(1, 8)
    // out: sum 1 and 8 is 9
}
*/

// 一次赋值的局部变量(val)
/*
fun main(args: Array<String>) {
    val a: Int = 1 // 立即赋值
    val b = 3 // 自动推断类型
    val c: Int // 未初始化值必须指定类型
    c = 4 // 直接赋值
    print("a = $a , b = $b , c = $c")
    // out: a = 1 , b = 3 , c = 4
}
*/

// 可变赋值(var)
/*
fun main(args: Array<String>) {
    var a = 3
    a += 1
    print("a = $a")
    // out: a = 4
}
*/

// 顶层变量
/*
val PI = 3.14 // 不可变
var x = 1 // 可变

fun incrementX() {
    x += 1
}

fun main(args: Array<String>) {
    println("original x = $x , PI = $PI")
    incrementX()
    println("Run incrementX() , x = $x , PI = $PI")
    */
/*
    out:original x = 1 , PI = 3.14
        Run incrementX() , x = 2 , PI = 3.14
     *//*

}
*/

// 字符串模板
/*
fun main(args: Array<String>) {
    var a = 1
    val s = "a is $a"
    a = 2
    val s1 = "${s.replace("is", "was")} , but now is $a"
    println(s1)
    // out: a was 1 , but now is 2
}
*/


